package com.bsb.biz.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bsb.base.common.config.Const;
import com.bsb.base.common.util.DateUtils;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.mapper.BsbUserMapper;
import com.bsb.base.model.BsbFirm;
import com.bsb.base.model.BsbOne;
import com.bsb.base.model.BsbOneExample;
import com.bsb.base.model.BsbOneExample.Criteria;
import com.bsb.biz.service.BsbFirmService;
import com.bsb.biz.service.BsbOneService;
import com.bsb.biz.service.BsbUserService;
import com.bsb.biz.service.SysUserService;
import com.bsb.biz.shiro.UserUtils;

@Controller
@RequestMapping("/bsbOne")
public class BsbOneController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbOne/list.do";
	
	@Resource
	private BsbOneService bsbOneService;
	
	@Resource
	private BsbUserService bsbUserService;
	@Resource
	private BsbFirmService bsbFirmService;
	@Resource
	private SysUserService sysUserService;
	
	
	@RequiresPermissions("bsbOne:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		String question = (String) params.get("question");
		String qname = (String) params.get("qname");
		String aname = (String) params.get("aname");
		String status = (String) params.get("status");
		String firm = (String) params.get("firm");
		String start = (String) params.get("start");
		Date startDate = DateUtils.parseDate(start);
		String end = (String) params.get("end");
		Date endDate = DateUtils.parseDate(end);
		BsbOneExample bsbOneExample = new BsbOneExample();
		Criteria criteria = bsbOneExample.createCriteria();
		Long id = UserUtils.currentUser().getFirmId();
		if(10000!=id){
			criteria.andFirmIdEqualTo(id.intValue());
		}
		if(StringUtils.isNotEmpty(question)){
			criteria.andQuestionLike("%"+question+"%");
		}
		if(StringUtils.isNotEmpty(qname)){
			List<String> list = bsbUserService.getIdsLikeName(qname);
			criteria.andPuserIdIn(list);
		}
		if(StringUtils.isNotEmpty(aname)){
			List<String> list = bsbUserService.getIdsLikeName(aname);
			if(null != list && list.size()>0){
				criteria.andCuserIdIn(list);
			}
		}
		if(StringUtils.isNotEmpty(firm)){
			List<Integer> list = bsbFirmService.getIdsLikeName(firm);
			if(null != list && list.size()>0){
				criteria.andFirmIdIn(list);
			}
		}
		if(StringUtils.isNotEmpty(status)){
			if("1".equals(status)){
				String[] ss = new String[]{"1","2","3","4","5"};
				List<String> list = Arrays.asList(ss);
				criteria.andStatusIn(list);
			}else{
				criteria.andStatusEqualTo(status);
			}
		}
		if(null != startDate){
			criteria.andCreateDateGreaterThanOrEqualTo(startDate);
		}
		if(null != endDate){
			endDate = DateUtils.addDays(endDate, 1);
			criteria.andCreateDateLessThanOrEqualTo(endDate);
		}
		int totalCount = bsbOneService.countBsbOne(bsbOneExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbOneExample.setPageIndex((pageNum - 1) * pageSize);
		bsbOneExample.setPageSize(pageSize);
		bsbOneExample.setOrderByClause("createDate DESC");
		List<BsbOne> bsbOnes = bsbOneService.getPageBsbOne(bsbOneExample);
		for (BsbOne bsbOne : bsbOnes) {
			String puserId = bsbOne.getPuserId();
			String cuserId = bsbOne.getCuserId();
			Integer firmId = bsbOne.getFirmId();
			bsbOne.setPuserName(bsbUserService.getUserNameById(puserId));
			bsbOne.setCuserName(bsbUserService.getUserNameById(cuserId));
			BsbFirm bsbFirmByAccount = bsbFirmService.getBsbFirmByAccount(firmId);
			if(null != bsbFirmByAccount){
				bsbOne.setFirmName(bsbFirmByAccount.getName());
			}
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbOnes", bsbOnes);
		model.addAttribute("question", question);
		model.addAttribute("qname", qname);
		model.addAttribute("aname", aname);
		model.addAttribute("status", status);
		model.addAttribute("firm", firm);
		model.addAttribute("start", start);
		model.addAttribute("end", end);
		return "/BsbOne/list";
	}
	
	@RequiresPermissions("bsbOne:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/BsbOne/add";
	}

	@RequiresPermissions("bsbOne:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbOne") BsbOne bsbOne) {
		bsbOneService.addBsbOne(bsbOne);
		return redirect;
	}
	
	@RequiresPermissions("bsbOne:delete")
	@RequestMapping(value = "/{uuid}/delete", method = RequestMethod.GET)
	public String del(Model model,
			@PathVariable("uuid") String uuid) {
		bsbOneService.delBsbOneByUuid(uuid);
		return redirect;
	}
	
	@RequiresPermissions("bsbOne:update")
	@RequestMapping(value = "/{uuid}/update", method = RequestMethod.GET)
	public String toEdit(Model model,
			@PathVariable("uuid") String uuid) {
		BsbOne bsbOne = bsbOneService.getBsbOneByUuid(uuid);
		model.addAttribute("bsbOne", bsbOne);
		return "/BsbOne/edit";
	}

	@RequiresPermissions("bsbOne:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model, @ModelAttribute("bsbOne") BsbOne bsbOne) {
		bsbOneService.updateBsbOne(bsbOne);
		return redirect;
	}
	
	@RequiresPermissions("bsbOne:view")
	@RequestMapping(value = "/{uuid}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("uuid") String uuid) {
		BsbOne bsbOne = bsbOneService.getBsbOneByUuid(uuid);
		String puserId = bsbOne.getPuserId();
		String cuserId = bsbOne.getCuserId();
		Integer firmId = bsbOne.getFirmId();
		bsbOne.setPuserName(bsbUserService.getUserNameById(puserId));
		bsbOne.setCuserName(bsbUserService.getUserNameById(cuserId));
		BsbFirm bsbFirmByAccount = bsbFirmService.getBsbFirmByAccount(firmId);
		if(null != bsbFirmByAccount){
			bsbOne.setFirmName(bsbFirmByAccount.getName());
		}
		model.addAttribute("bsbOne", bsbOne);
		return "/BsbOne/view";
	}
}
